const JPush = require("jpush-async/lib/JPush/JPushAsync")
const JpushsHelp = require("../database/jpush/JpushsHelp")




//华人街
const appKey='c5b1127f713dae43bdcec2cc'
const masterSecret='1fa8d0af50d955381edd8918'



const client = JPush.buildClient(appKey, masterSecret)

const userHelp=require('../database/users/userHelp')



//easy push
const push=async function(uid,unReadNum,extras,user_agent,socket,pushType,gid){

    //用户不存在聊天服务器，不用推送极光
    let is_exist=await userHelp.isUserExist(uid-0)
    if(!is_exist){
        return
    }


    //推送是否开启，总开关
    let res=await JpushsHelp.getJpushState(uid-0)
    let is_on=1
    if(res){
        is_on=res.is_on
    }
    if(is_on==0){
        return
    }


    //群推送是否开启
    if(pushType==3){   //pushType=3为群推送，1为单聊推送
        let group_is_on=await JpushsHelp.getGroupJpush(uid-0,gid-0)
        if(group_is_on==0){
            return
        }
    }



    if(user_agent=='android'){   //user_agent 发送者的手机系统
        client.push().setPlatform('ios','android')
            .setAudience(JPush.alias(uid))
            .setNotification(JPush.ios('你收到一条新消息', null, '+1',null,extras),JPush.android('你收到一条新消息', null, 1,extras))
            .send()
            .then(function(result) {
                console.log(result)
            }).catch(function(err) {
            console.log(err)
        })
    }
    else{
        client.push().setPlatform('ios','android')
            .setAudience(JPush.alias(uid))
            .setNotification(JPush.ios('你收到一条新消息', null, '+1',null,extras),JPush.android('你收到一条新消息', null, 1,extras))
            .setOptions(null, null,null,true)   //true:生产环境，  false:开发环境
            .send()
            .then(function(result) {
                console.log(result)
            }).catch(function(err) {
               console.log(err)
        })
    }

}




module.exports={
    push:push,
}
